﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Objects;
using System.Collections.Generic;
using BAL;

public partial class Admin_UCControls_UCChiNhanh : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetTinhThanh();
            GetChinhanByIdTinhThanh(0);
        }
    }

    private void GetTinhThanh()
    {
        List<TinhThanh> ListTinhThanh = new List<TinhThanh>();
        ListTinhThanh = BAL.BALTinhThanh.GetAllItem();

        TinhThanh item = new TinhThanh();

        item.Id = 0;
        item.TenTinhThanh = "Chọn Tỉnh Thành";

        ListTinhThanh.Insert(0, item);

        ddlTinhThanh.DataTextField = "TenTinhThanh";
        ddlTinhThanh.DataValueField = "Id";
        ddlTinhThanh.DataSource = ListTinhThanh;
        ddlTinhThanh.DataBind();

    }

    public static List<ChiNhanh> ListChiNhanh= new List<ChiNhanh>();


    private void GetChinhanByIdTinhThanh(int IdTinhThanh)
    {
        ListChiNhanh = BAL.BALChiNhanh.GetByIdTinhThanh(IdTinhThanh);

        if (ListChiNhanh.Count > 0)
        {
            GridChiNhanh.DataSource = ListChiNhanh;
            
        }
        GridChiNhanh.PageIndex = 0;
        GridChiNhanh.DataBind();
      
    }

    private void GetChinhanByTenChiNhanh(string ten)
    {
        ListChiNhanh = BAL.BALChiNhanh.GetByTenChiNhanh(ten);

        if (ListChiNhanh.Count > 0)
        {
            GridChiNhanh.DataSource = ListChiNhanh;
        }
        GridChiNhanh.PageIndex = 0;
        GridChiNhanh.DataBind();
    }

    protected void GridChiNhanh_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Sua")
        {

            foreach (ChiNhanh Item in ListChiNhanh)
            {
                if (Item.Id == Convert.ToInt16(e.CommandArgument))
                {
                    Session.Add("ItemChiNhanh", Item);
                }
            }

            Response.Redirect("../Admin/ChiNhanh_Update.aspx");

        }

        if (e.CommandName == "Xoa")
        {
            int item;
            item = BALChiNhanh.Delete_ChiNhanh(Convert.ToInt32(e.CommandArgument.ToString()));
            GetChinhanByIdTinhThanh(0);
        }
    }

    protected void btnThemMoi_Click(object sender, EventArgs e)
    {
        Session.Remove("ItemChiNhanh");
        Response.Redirect("../Admin/ChiNhanh_Update.aspx");
    }

    protected void btnTim_Click(object sender, EventArgs e)
    {
        string Temp = ddlTinhThanh.SelectedValue.ToString();
        if (txtTenChiNhanh.Text != "")
            GetChinhanByTenChiNhanh(txtTenChiNhanh.Text);
        else
            GetChinhanByIdTinhThanh(Convert.ToInt16(Temp));

    }

    protected void GridChiNhanh_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
            GridChiNhanh.DataSource = ListChiNhanh;
            GridChiNhanh.PageIndex = e.NewPageIndex;
            GridChiNhanh.DataBind();
    }
}
